<!-- 组件说明 -->
<template>
  <div class="roles">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>

      <!-- 角色列表区域 -->
      <el-table
        :data="rolesList"
        row-key="id"
        border
        stripe
      >
        <el-table-column type="expand">
          <template slot-scope="scope">
            <!-- 循环得到所有的权限 -->
            <el-row
              v-for="(item1, i1) in scope.row.children"
              :key="item1.id"
              :class="['vcenter', 'bdbottom', i1 === 0 ? 'bdtop' : '']"
            >
              <!-- 所有的一级权限 -->
              <el-col :span="5">
                <el-tag
                  closable
                  @close="removeRightById(scope.row, item1.id)"
                >{{ item1.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 所有的二三级权限 -->
              <el-col :span="19">
                <el-row
                  v-for="(item2, i2) in item1.children"
                  :key="item2.id"
                  :class="['vcenter', i2 === 0 ? '' : 'bdtop']"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="removeRightById(scope.row, item2.id)"
                    >{{ item2.authName }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      type="warning"
                      v-for="(item3) in item2.children"
                      :key="item3.id"
                      closable
                      @close="removeRightById(scope.row, item3.id)"
                    >
                      {{ item3.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column
          label="角色名称"
          prop="roleName"
        ></el-table-column>
        <el-table-column
          label="角色描述"
          prop="roleDesc"
        ></el-table-column>
        <el-table-column
          label="操作"
          width="600px"
        >
          <!-- 放置三个操作按钮 -->
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
            >编辑</el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
            >删除</el-button>
            <el-button
              type="warning"
              icon="el-icon-setting"
              @click="showSetRightDialog(scope.row)"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 选择权限对话框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="setRightDialogVisible"
      width="30%"
    >
      <!-- 树型控件 -->
      <el-tree
        :data="rightlist"
        :props="treeProps"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="defCheckedArr"
        ref="treeRef"
      ></el-tree>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="setRightDialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="allotRights"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// import x from ''
export default {
  components: {

  },
  data () {
    return {
      rolesList: [], // 角色列表
      setRightDialogVisible: false,
      rightlist: [], // 权限树列表数据
      treeProps: { // 树形控件的属性设置
        label: 'authName',
        children: 'children'
      },
      defCheckedArr: [], // 默认选中的权限
      roleId: 0 // 当前将分配权限的roleId
    }
  },
  computed: {

  },
  mounted () {
    this.getRoleList()
  },
  methods: {
    async getRoleList () {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) {
        return this.$message.error('获取角色列表失败')
      }
      this.rolesList = res.data
      console.log(this.rolesList, 'this.rolesList')
    },
    // 根据id删除对应的权限
    async removeRightById (role, rightId) {
      // 弹出确认提示框
      const confirmResult = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('取消了删除')
      }

      console.log('点击了删除确认')
      const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${rightId}`)
      if (res.meta.status !== 200) {
        this.$message.error('删除权限失败')
      }
      role.children = res.data
    },
    // 添加选择权限事件
    async showSetRightDialog (role) {
      this.roleId = role.id
      this.defCheckedArr = []
      this.getLeafKeys(role, this.defCheckedArr)
      this.setRightDialogVisible = true
      // 获取权限数据
      const { data: res } = await this.$http.get('rights/tree')
      console.log(res, '权限数据')
      this.rightlist = res.data
    },
    // 递归获取所有三级权限的id
    getLeafKeys (node, arr) {
      // 三级权限没有children属性
      if (!node.children) {
        return arr.push(node.id)
      }
      // 否则的话递归
      node.children.forEach(element => {
        this.getLeafKeys(element, arr)
      })
    },
    // 点击确定，分配权限
    async allotRights () {
      // 先获取到选中的权限，拼接成字符串样式
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      const idStr = keys.join(',')
      console.log(keys, 'keys')
      // 调用接口传给后台
      const { data: res } = await this.$http.post(`roles/${this.roleId}/rights`, { rids: idStr })
      if (res.meta.status !== 200) {
        return this.$message.error('角色授权失败')
      }

      this.$message.success('角色授权成功')
      this.getRoleList()
      this.setRightDialogVisible = false
    }
  }
}
</script>

<style lang='less' scoped>
//@import url()
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eeeeee;
}

.bdbottom {
  border-bottom: 1px solid #eeeeee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>
